/**
 * 
 */
package org.maurange.formation.licpro;

import java.util.ArrayList;

import org.maurange.formation.licpro.rest.Arret;
import org.maurange.formation.licpro.rest.ArretDetails;
import org.maurange.formation.licpro.rest.ArretDetailsRestMethod;
import org.maurange.formation.licpro.rest.ArretRestMethod;
import org.maurange.formation.licpro.rest.ListArret;
import org.maurange.formation.licpro.rest.ListArretDetails;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.location.Address;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

/**
 * @author E094230Z
 *
 */
public class ArretDetailsActivity extends ListActivity
{
	private String codeLieu = null;
	private ArretAdapterDetails arretAdapter;
	private static ListDetailsLoadAsyncTask asyncTask = null;
	
	 @Override
	 protected void onCreate(Bundle savedInstanceState)
	 {
	        super.onCreate(savedInstanceState);
	        this.arretAdapter = new ArretAdapterDetails(this, new ArrayList<ArretDetails>());
	        setListAdapter(arretAdapter);
	        Bundle recupInfos = getIntent().getExtras();
	        this.codeLieu = recupInfos.getString("code");
	        
	        this.asyncTask = new ListDetailsLoadAsyncTask(this.arretAdapter);
	        
	        
	 }
	 
	 @Override
	protected void onResume()
 	{
		super.onResume();
		
		//TODO     
        if (this.asyncTask == null)
        {
        	this.asyncTask = new ListDetailsLoadAsyncTask(arretAdapter);
        	this.asyncTask.attach(this);
        	asyncTask.execute(this.codeLieu);
        }
        
        else
        {
        	asyncTask.detach();
        	asyncTask.cancel(true);
        	this.asyncTask = new ListDetailsLoadAsyncTask(arretAdapter);
        	this.asyncTask.attach(this);
        	asyncTask.execute(this.codeLieu);
        }
		
	}
	 
	 static private class ListDetailsLoadAsyncTask extends AsyncTask<String, Integer, ListArretDetails>
		{
			private Activity activity;
			private ArretAdapterDetails adapter;
			
			public ListDetailsLoadAsyncTask(ArretAdapterDetails adapt)
			{
				super();
				this.adapter = adapt;
			}

			protected void onPostExecute(ListArretDetails list)
			{
				this.adapter.getListLignes().addAll(list);
				this.adapter.notifyDataSetChanged();
			}

			/*
			protected void onProgressUpdate(Double... tabArg)
			{
			}*/
			
			@Override
			protected ListArretDetails doInBackground(String... tabArg)
			{
				ArretDetailsRestMethod arm = new ArretDetailsRestMethod(activity);
				ListArretDetails list = arm.getArretsRest(tabArg[0]);
				return list;
			}
			
			public void detach()
			{
				this.activity = null;
			}
			
			public void attach(Activity act)
			{
				this.activity = act;
			}
			
		}
}
